
// 对象转化成数组
function setArry (data) {
  if (!data) return
  let setArryData = []
  if (data instanceof Array) {
    return data
  }
  Object.values(data).forEach(item => {
    if (item.children instanceof Object) {
      item.children = setArry(item.children)
    }
    if (item.childrens instanceof Object) {
      item.childrens = setArry(item.childrens)
    }
    item.title = item.name
    setArryData.push(item)
  })
  return setArryData
}



// 公司
function formatCompanyData (data, t, id = 0, sArr = []) {
  if (!data) return
  Object.keys(data).map(k => {
    data[k].ftype = t
    data[k].name ? data[k].title = data[k].name : ''
    data[k].spread = true
    data[k].pid = id
    if (data[k].hasOwnProperty('children') && Object.keys(data[k].children).length > 0) {
      formatCompanyData(data[k].children, t, data[k].id)

      data[k].childrens = Object.values(data[k].children)
      if (data[k].type === 'company' || data[k].type === 'customer') {
        let arr = []
        Object.values(data[k].children).map(obj => {
          if (obj.type === 'company' || obj.type === 'customer') {
            arr.push(obj)
          }
        })
        data[k].children = arr
      }
    }
  })
  return Object.values(data)
}

// 客商
function formatCustomerData (data, t, id, sArr) {
  let arr = []
  if (!data) return
  Object.values(data).map(obj => {
    obj.ftype = t
    obj.pid = id
    obj.title = obj.name
    arr.push(obj)
  })
  return arr
}


// 部门
function formatDepartmentData (data, isChecked) {
  if (!data) {
    return []
  }
  let arr = []
  data.map(obj => {
    if (obj.type !== 'company') {
      if (obj.hasOwnProperty('childrens') && obj.childrens.length > 0) {
        obj.children = obj.childrens
        formatDepartmentData(obj.childrens, isChecked)
      }
      arr.push(obj)
    }
  })
  return arr
}

// 获取重载树的数据
function getResetData (data, pid, callback) {
  for (let i = 0; i < data.length; i++) {
    if (data[i].id === pid) {
      callback && callback(data[i])
    } else {
      if (data[i].childrens) getResetData(data[i].childrens, pid, callback)
      if (data[i].children) getResetData(data[i].children, pid, callback)
    }
  }
}

// 查找子节点并赋当前checked状态
function findChildData (data, tree) {
  let list1 = data?.children || [] // 子节点数据
  let list2 = data?.childrens || [] // 下级树数据
  for (let i = 0; i < list1.length; i++) {
    list1[i].checked = data.checked
    if (data.type !== list1[i].type) resetTree(list1[i], tree)
    if (list1[i].children) findChildData(list1[i], tree)
  }
  for (let j = 0; j < list2.length; j++) {
    list2[j].checked = data.checked
    if (data.type !== list2[j].type) resetTree(list2[j], tree)
    if (list2[j].childrens) findChildData(list2[j], tree)
  }
}

// 重载树
function resetTree (data, tree) {
  let status = false // 防抖暂时未加
  let type = data.type

  setTimeout(() => {
    getResetData(baseData, data.pid, function (itemData) {
      tree.reload(getTreeId(type), { data: itemData.childrens });
    })
  })

}

// 根据type获取对应树的id名
function getTreeId (key) {
  let typeData = {
    index: 'demoId1',
    customer: 'demoId2',
    department: 'demoId3',
    company: 'demoId2'
  }
  return typeData[key]
}